;; IDL scripts and data needed for a minimal working example on how to compute the succesive slits used in the data analysis pertaining to:
;; "The Trigger Mechanism of Recurrent Active Region Jets Revealed by the Magnetic Properties of a Coronal Geyser Site"; APJ submitted
;; Author: Alin Paraschiv; alin.paraschiv "at" monash.edu
;; Date: 20 Oct 2019

;; This example file contains some direct IDL code use examples, explanations and assumes in part a basic Solarsoft (SSWIDL) path is present.
;; Keep in mind that the modules showcased here also contain their own coments/documentation.


;; Restore the datacube 
;; There are 110 frames corresponding to the full day of observation (25 Sep. 2011). Datagaps are present between 06:00TAI-07:36TAI and 10:12TAI
restore, './AR11302_b_sharp_disambig_fullday.dat',/v
;;% RESTORE: Restored variable: BX.           ;;heliographic horizontal field
;;% RESTORE: Restored variable: BXE.          ;;heliographic horizontal field error
;;% RESTORE: Restored variable: BY.           ;;heliographic horizontal field
;;% RESTORE: Restored variable: BYE.          ;;heliographic horizontal field error
;;% RESTORE: Restored variable: BZ.           ;;heliographic vertical field
;;% RESTORE: Restored variable: BZE.          ;;heliographic vertical field error
;;% RESTORE: Restored variable: X_MGM.        ;;x coordinate axis (in arcsec)
;;% RESTORE: Restored variable: Y_MGM.        ;;y cordinate axis  (in arcsec)
;;% RESTORE: Restored variable: B_TRANS.      ;;input data: transversal fields    (from the JSOC B, phi and gamma quantities)
;;% RESTORE: Restored variable: B_LONG.       ;;input data: longitudinal fields   (from the JSOC B, phi and gamma quantities)
;;% RESTORE: Restored variable: B_PHI.        ;;input data: azimuth fields        (from the JSOC B, phi and gamma quantities)
;;% RESTORE: Restored variable: B_AZIM.       ;;input data: custom disambig azimuth fields
;;% RESTORE: Restored variable: HEADER_B.     ;;JSOC header for B  
;;% RESTORE: Restored variable: HEADER_PHI.   ;;JSOC header for phi
;;% RESTORE: Restored variable: HEADER_GAMMA. ;;JSOC header for gamma

;; Most HMI plots presented in the paper can be directly reproduced from the BZ cube that is loaded above.
;; More complex scripts are provided and described below.

;; The slit analysis is the main method used in the accompanying paper.

;;Restore the needed functions to compute the slits.
.r aia0_arc2pix         ;; small tool for converting between image and physical coordinates using the image header.
.r aia0_rdpix           ;; small tool to read coordinates. Adapted from the very old idl rdpix routine.
.r aia0_mkslit          ;; computes the slit, (at any angle) using spatial gradients/determinants.
.r img0_plotslit        ;; main script for plotting the slit information.  
.r img0_tracksubreg     ;; main script for defining and tracking a subregion in time using the SSWIDL rot_xy routine.  

;;In order to minimally reproduce figure 6 and the plots found in appendix c of www.doi.org/10.26180/5bc9d76627396 one would just run:
img0_plotslit,i1=0,i2=16,ind=header_b,dat=bz
;; the routine takes as input the start and end frame used to compute the slit, the header (needs to be complete for all timesteps), and 3d timeseries datacube cube[x,y,t].

;; to reproduce two (or more) slits in one plot requires a very simple tweak where you save the output to two variables and redo just the plotting e.g.
;; img0_plotslit,0,16,ind=header_b,dat=bz
;; slit1=slit
;; img0_plotslit,0,16,header_b,bz
;; slit2=slit
;; Then just take the plotting script at the end and add two slit plots, then set the positions accordingly. 
;; Setting the slit positions in the same places as shown in figure 6 of the associated paper will reproduce the plot.


;; To reproduce the B1 region or to track it in time using HMI, or even AIA data (this needs to be downloaded separately due to size constraints) one can use the img0_tracksubreg.pro.

;; The central tracked coordinate of an example ~20x30 arcsec region is manually defined along with its size. 
;; De-comment the specific aia0_rdpix part to click on a region or feature to be tracked in time.
;; Important!!!!!!!!!!! 
;; This routine should not be used on sharp data, as rotation is already accounted. We only use it as an example on the available data.
img0_tracksubreg,ind=header_b,dat=bz

;; The function will return two variables dat_t[x_cut,y_cut,t] (a 3d cube of the cut and temporally tracked region), and dn_t[flux,t](a pixel average flux count). If one wishes to return just the coordinates of the region, the P1 and box0/box1 variables can be used. 
;; This allows the full reproduction of Figure 2 (with additional AIA data download) and the locations of the B1 region in all figures. 

;; We also include the data (AR11302_extrap_pot_fullres_a=0_2011.09.25_01:12TAI) that can be used to reproduce the potential field extrapolation presented in Figure 4. We utilized the Paraview software and use the netcdf generic format as the reading input. 


;; Additional notes:
;; We have primarily used the BZ data as the BX and BY were seriously affected by noise, making them unreliable in our oppinion. The data cubes can be used to check this.
;; Additional processing to remove fine tracking issues ("pixel jumps","bad pixels" etc.) are not included in this minimal example. 
;; The disambiguation result as viewed for the entire active region is not perfect. The setup was chosen as to retrieve the best processed data inside our region of interest (B1). The parameter setup might lead to degradation of quality in other regions that were not our focus. More details can be found in the accompanying paper.